Component({
  options: {
    pureDataPattern: /^_/ // 指定所有 _ 开头的数据字段为纯数据字段
  },

  data: {
    address: '点击选择，要勾选哦~',
    isSubmit: false,
    isSucc: false,
    _latitude: 0,
    _longitude: 0
  },

  methods: {
    handleAddressTap() {
      wx.chooseLocation({
        success: ({address, latitude, longitude}) => {
          this.setData({
            address,
            _latitude: latitude,
            _longitude: longitude
          })
        }
      })
    },

    submit(e) {
      let {address, _latitude, _longitude} = this.data
      const id = Math.floor(Math.random() * 1000000000) + new Date().getTime()
      const data = {
        ...e.detail.value,
        address,
        latitude: _latitude,
        longitude: _longitude,
        id
      }

      const db = wx.cloud.database()
      db.collection('minipet').add({
        data,
        success: (res) => {
          this.setData({
            isSucc: true
          })
        },

        fail: () => {
          this.setData({
            isSucc: false
          })
        },

        complete: () => {
          this.setData({
            isSubmit: true
          })
        }
      })
    },

    handleChangeSucc(e) {
      this.setData({
        isSucc: e.detail,
        isSubmit: e.detail
      })
    }
  }
})